import 'package:dio/dio.dart';
import 'package:get/get.dart';
import '../data/local/preference/preference_manager.dart';
import '../data/local/preference/preference_manager_impl.dart';

class RequestHeaderInterceptor extends InterceptorsWrapper {
  final PreferenceManagerImpl preferenceManager = Get.put(PreferenceManagerImpl());

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    getCustomHeaders().then((customHeaders) {
      options.headers.addAll(customHeaders);
      super.onRequest(options, handler);
    });
  }

  Future<Map<String, String>> getCustomHeaders() async {
    String token = await preferenceManager.getString(PreferenceManager.keyToken);
    
    var customHeaders = {'content-type': 'application/json'};
    
    if( token != "" ){
      customHeaders.addAll({"Authorization": "Bearer ${token}"});
    }
    customHeaders.addAll({"Access-source": "gczn-execute"});

    return customHeaders;
  }
}
